//
//  ClassificationController.m
//  OCDemo
//
//  Created by zhumuzhi on 2021/12/24.
//

#import "ClassificationController.h"
#import "BYClassificationHeader.h"
#import "BYClassificationItem.h"

@interface ClassificationController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataList;

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ClassificationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
{
    return self.dataList.count;
}

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSDictionary *sectionData = self.dataList[section];
    NSArray *sectionList = sectionData[@"dataList"];
    return sectionList.count;
}

- (__kindof UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
    BYClassificationItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BYClassificationItemID" forIndexPath:indexPath];
    NSDictionary *sectionData = self.dataList[indexPath.section];
    NSArray *sectionList = sectionData[@"dataList"];
    NSDictionary *data = sectionList[indexPath.row];
    cell.data = data;
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath
{
    return CGSizeMake(kScreenWidth/2 - kAutoWithSize(20), kAutoWithSize(30));
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        BYClassificationHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BYClassificationHeaderID" forIndexPath:indexPath];
        NSDictionary *data = self.dataList[indexPath.section];
        header.data = data;
        return header;
    }
    return nil;
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kScreenWidth, [BYClassificationHeader CellHeight]);
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
    
}


- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing  = kAutoWithSize(10);
        flowLayout.minimumInteritemSpacing = kAutoWithSize(10);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(kAutoWithSize(10), 0, kScreenWidth-kAutoWithSize(20), kScreenHeight-kSafeAreaTopHeight-kTabBarHeight-kSafeAreaBottomHeight) collectionViewLayout:flowLayout];
        _collectionView.dataSource=self;
        _collectionView.delegate=self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[BYClassificationItem class] forCellWithReuseIdentifier:@"BYClassificationItemID"];
        [_collectionView registerClass:[BYClassificationHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BYClassificationHeaderID"];
        
    }
    return _collectionView;
}

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [NSMutableArray arrayWithArray:@[@{@"sectionTitle":@"人群分类", @"dataList":@[@{@"title":@"学龄前"},@{@"title":@"儿童"},@{@"title":@"成人系列"}]},@{@"sectionTitle":@"系列",@"dataList":@[@{@"title":@"乐高城市组"},@{@"title":@"乐高建筑系列"},@{@"title":@"乐高创意百变组"}]}]];
    }
    return _dataList;
}



@end
